/**
 * 
 */
package edu.hunau.dom4j;

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import edu.hunau.pojo.Book;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.junit.Ignore;
import org.junit.Test;

/**
 * dom4j解析xml
 *
 * https://dom4j.github.io/
 * @author hb
 */
public class XmlDom4j {
	@Test
	public void testCreate() {
		createXML();
	}

	@Test
	public void testReader() {
		readXML("./src/main/resources/books.xml");
	}

	/** 读取XML文档 */
	public void readXML(String fileName) {
		SAXReader reader = new SAXReader();
		try {// 读取指定的文件
			Document document = reader.read(fileName);
			// 获得根元素
			Element root = document.getRootElement();
			System.out.println(root.getQName().getName());
			// 获得所有的子节点
			List<Element> childs = root.elements();
			for (Element child : childs) {
				System.out.println("id=" + child.attributeValue("id"));
				String author = child.element("author").element("name").getText();
				System.out.println("\tauthor=" + author);
				String name = child.elementText("name");
				System.out.println("\tname=" + name);
				String price = child.elementText("price");
				System.out.println("\tprice=" + price);
			}
		} catch (DocumentException e) {
			throw new RuntimeException(e);
		}
	}

	public void createXML() {
		// 准备的数据
		List<Book> books = initData();
		// 1.获得com.dom4j.Document
		Document document = DocumentHelper.createDocument();
		// 2.创建元素并且添加,根元素有且只有一个
		Element root = document.addElement("books");
		for (int i = 0, length = books.size(); i < length; i++) {
			Book data = books.get(i);
			// 3.为根元素添加节点
			Element book = root.addElement("book");
			Element author = book.addElement("author");
			// author中的子元素name
			Element aName = author.addElement("name");
			Element bname = book.addElement("name");
			Element price = book.addElement("price");

			// 4.设置book的id属性
			book.addAttribute("id", data.getId() + "");
			// 5.设置文本内容
			aName.setText(data.getAuthor());
			bname.setText(data.getName());
			price.setText(data.getPrice() + "");

		}
		createFile(document, "./src/main/resources/books.xml");
	}

	private void createFile(Document document, String fileName) {
		XMLWriter xmlWriter = null;
		try {
			// 定义输出流
			FileWriter fw = new FileWriter(fileName);
			// 定义输出格式,可读性高的
			OutputFormat format = OutputFormat.createPrettyPrint();// OutputFormat.createCompactFormat();
			// 获得dom4j提供生成xml文件的流
			xmlWriter = new XMLWriter(fw, format);
			xmlWriter.write(document);

		} catch (IOException e) {
			throw new RuntimeException(e);
		} finally {
			if (xmlWriter != null) {
				try {
					xmlWriter.flush();
					xmlWriter.close();
				} catch (Exception e2) {
					throw new RuntimeException(e2);
				}
			}
		}

	}

	private List<Book> initData() {
		List<Book> books = new ArrayList<Book>();
		books.add(new Book(1001, "haoren", "如何写好一个Bug", 150));
		books.add(new Book(1002, "二月河", "康熙王朝", 50));
		books.add(new Book(1003, "Jeson", "spring", 88));
		books.add(new Book(1004, "布鲁克斯", "人月神话", 120));
		books.add(new Book(1005, "刘奕群", "搜索引擎技术基础", 53));
		return books;
	}
}
